package com.hsteach.enums;

import com.hsteach.common.exception.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@AllArgsConstructor
public enum UnitTypeEnum {

    SCHOOL(0, "学校"),
    SECTION_GRADE(1, "学段"),
    GRADE(2, "年级"),
    CLASS(3, "班级"),
    ;

    private Integer code;
    private String name;

    public static List<Integer> getCodesByWord(String searchWord) {
        return Arrays.stream(UnitTypeEnum.values())
                .filter(e -> e.getName().contains(searchWord))
                .map(UnitTypeEnum::getCode)
                .collect(Collectors.toList());
    }

    public static String getNameByCode(Integer code) {
        UnitTypeEnum interestEnum = Arrays.stream(UnitTypeEnum.values())
                .filter(e -> e.getCode().equals(code))
                .findFirst().orElse(null);
        if (interestEnum == null) {
            throw new ServiceException("不存在的tag枚举：" + code);
        }
        return interestEnum.getName();
    }
}
